<template>
  <LayoutContent>
    <div class="button">
      <div class="detail">{{ $route.query.id ? '编辑' : '新增' }}</div>
      <div>
        <el-button type="primary" @click="clickHandle('save')">保存</el-button>
        <el-button type="primary" @click="clickHandle('rest')">重置</el-button>
        <el-button @click="clickHandle('back')">返回</el-button>
      </div>
    </div>
    <div class="form">
      <el-form ref="formRef" :model="form" status-icon :rules="rules" label-position="left" label-width="auto">
        <el-form-item label="学号" prop="id">
          <el-input disabled v-model.trim="form.id" placeholder="" />
        </el-form-item>
        <el-form-item label="账号：" prop="account">
          <el-input disabled v-model.trim="form.account" placeholder="" />
        </el-form-item>
        <el-form-item label="姓名：" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model.trim="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="届时" prop="year">
          <el-date-picker v-model="form.year" value-format="YYYY" format="YYYY" type="year" placeholder="请选择届时" />
        </el-form-item>
        <el-form-item label="性别：" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="专业：" prop="majorId">
          <el-select v-model="form.majorId" filterable placeholder="请选择专业">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in majorList" :key="index" />
          </el-select>
        </el-form-item>
        <el-form-item label="班级：" prop="classId">
          <el-select v-model="form.classId" filterable placeholder="请选择班级">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in classList" :key="index" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model.trim="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model.trim="form.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
    </div>
  </LayoutContent>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import { addApi, getDetailApi, getMajorListApi, getClassListApi } from '@/api/student/index'
import { useRoute, useRouter } from 'vue-router'
const isTea = inject('isTea')

const rules = reactive({
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  majorId: [{ required: true, message: '请选择专业', trigger: 'blur' }],
  classId: [{ required: true, message: '请选择专业', trigger: 'blur' }],
  year: [{ required: true, message: '请选择届时', trigger: 'blur' }],
  phone: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确格式的手机号', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, message: '请输入正确格式的邮箱', trigger: 'blur' },
  ],
})

const form = ref({})
const formRef = ref(null)

const router = useRouter()
const route = useRoute()
const clickHandle = (type) => {
  switch (type) {
    case 'back':
      router.back()
      break
    case 'save':
      submit()
      break
    case 'rest':
      formRef.value.resetFields()
      break
  }
}

onMounted(() => {
  if (route.query.id) {
    getDetail()
  }
  getMajorList()
  getClassList()
})

// 获取详情
const getDetail = async () => {
  const res = await getDetailApi({ id: route.query.id })
  if (res.code == 200) {
    form.value = res.data
  } else {
    ElMessage.error(res.msg)
  }
}

const majorList = ref([])
const getMajorList = async () => {
  const res = await getMajorListApi()
  if (res.code == 200) {
    majorList.value = res.data
  } else {
    ElMessage.error('获取专业列表数据失败')
  }
}

const classList = ref([])
const getClassList = async () => {
  const res = await getClassListApi()
  if (res.code == 200) {
    classList.value = res.data
  } else {
    ElMessage.error('获取班级列表数据失败')
  }
}

// 提交
const submit = async () => {
  if (!formRef.value) return
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await addApi(form.value)
      if (res.code == 200) {
        ElMessage.success(res.msg)
        router.back()
      } else {
        ElMessage.error(res.msg)
      }
    }
  })
}
</script>

<style scoped lang="scss">
.content {
  .card {
    height: 100%;

    .button {
      width: 100%;
      display: flex;
      justify-content: space-between;
      padding-bottom: 10px;
      border-bottom: 2px solid #eee;
    }
    .form {
      margin-top: 10px;
    }
  }
}
</style>
